﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Caching;

namespace Rogan.Utility.Common
{
    /// <summary>
    /// CacheHelper Provide the basic function
    /// </summary>
    public static class CacheHelper
    {
        private static readonly Cache Cache;
        private const int DefaultSeconds = 10;
        static CacheHelper()
        {
            HttpContext current = HttpContext.Current;
            if (current != null)
            {
                Cache = current.Cache;
                return;
            }
            Cache = HttpRuntime.Cache;
        }
        public static void Clear()
        {
            var enumerator = Cache.GetEnumerator();
            var arrayList = new List<object>();
            while (enumerator.MoveNext())
            {
                arrayList.Add(enumerator.Key);

            }
            foreach (string key in arrayList)
            {
                Cache.Remove(key);
            }
        }
        public static void RemoveByPattern(string pattern)
        {
            var enumerator = Cache.GetEnumerator();
            var regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Singleline);
            while (enumerator.MoveNext())
            {
                if (regex.IsMatch(enumerator.Key.ToString()))
                {
                    Cache.Remove(enumerator.Key.ToString());
                }
            }
        }
        public static void Remove(string key)
        {
            Cache.Remove(key);
        }
        public static void Insert(string key, object obj)
        {
            Insert(key, obj, null, DefaultSeconds);
        }
        public static void Insert(string key, object obj, CacheDependency dep)
        {
            Insert(key, obj, dep, DefaultSeconds);
        }
        public static void Insert(string key, object obj, int seconds)
        {
            Insert(key, obj, null, seconds);
        }
        public static void Insert(string key, object obj, int seconds, CacheItemPriority priority)
        {
            Insert(key, obj, null, seconds, priority);
        }
        public static void Insert(string key, object obj, CacheDependency dep, int seconds)
        {
            Insert(key, obj, dep, seconds, CacheItemPriority.Normal);
        }
        public static void Insert(string key, object obj, CacheDependency dep, int seconds, CacheItemPriority priority)
        {
            if (obj != null)
            {
                Cache.Insert(key, obj, dep, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero, priority, null);
            }
        }
        public static void Max(string key, object obj)
        {
            Max(key, obj, null);
        }
        public static void Max(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                Cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.AboveNormal, null);
            }
        }
        public static void Permanent(string key, object obj)
        {
            Permanent(key, obj, null);
        }
        public static void Permanent(string key, object obj, CacheDependency dep)
        {
            if (obj != null)
            {
                Cache.Insert(key, obj, dep, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.NotRemovable, null);
            }
        }
        public static object Get(string key)
        {
            return Cache[key];
        }
    }


    public class CacheInfo
    {
        public int? TimeOut
        {
            get;
            set;
        }
        public string FilePath
        {
            get;
            set;
        }
        public bool IsNullOrEmpty()
        {
            return !TimeOut.HasValue && string.IsNullOrEmpty(FilePath);
        }
    }
}
